﻿using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Media;

namespace WpfMyText
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application,INotifyPropertyChanged
    {
        private static double textFontSize ;
        private static object textFontFamily;
        private static Color backgroundColor ;
        private static double backgroundOpacity ;
        private static double windowWidth   ;
        private static double windowHeight;
        public double WindowWidth
        {
            get { return windowWidth; }
            set{
                windowWidth = value;
                OnPropertyChanged();
            }
        }

        public double WindowHeight
        {
            get
            {
                return windowHeight;
            }
            set
            {
                windowHeight = value;
                OnPropertyChanged();
            }
        }


        public double TextFontSize {
            get
            {
                return textFontSize;
            }
            set
            {
                textFontSize = value;
                OnPropertyChanged();

            }
        
        }
        public object TextFontFamily
        {
            get
            {
                return textFontFamily;
            }
            set
            {
                textFontFamily = value;
                OnPropertyChanged();
            }
        }
     
        public  Color  BackgroundColor 
        {
            get
            {
                return backgroundColor;
            }
            set
            {
                backgroundColor = value;
                OnPropertyChanged();
            }
        
        
        }
       
    
        public double BackgroundOpacity
        {
            get
            {
                return backgroundOpacity;
            }
            set
            {
                backgroundOpacity = value;
                OnPropertyChanged();
            }
        }



        public void SaveUserProperties()
        {
            try
            {
                // 获取配置文件
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                // 更新字体大小配置项
                config.AppSettings.Settings["TextFontSize"].Value = TextFontSize.ToString();
                // 更新字体族配置项
                config.AppSettings.Settings["TextFontFamily"].Value = TextFontFamily.ToString();
                config.AppSettings.Settings["BackgroundColor"].Value = BackgroundColor.ToString();
                config.AppSettings.Settings["BackgroundOpacity"].Value = BackgroundOpacity.ToString();
                config.AppSettings.Settings["WindowWidth"].Value = WindowWidth.ToString();
                config.AppSettings.Settings["WindowHeight"].Value = WindowHeight.ToString();


                // 保存配置文件
                config.Save(ConfigurationSaveMode.Modified);
                // 刷新配置，使更改生效
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存用户属性时出错: {ex.Message}");
            }
        }

        public void LoadUserProperties()
        {
            try
            {
                // 从配置文件中读取字体大小
                if (double.TryParse(ConfigurationManager.AppSettings["TextFontSize"], out double fontSize))
                {
                    TextFontSize = fontSize;
                }
                // 从配置文件中读取字体族
                string fontFamilySource = ConfigurationManager.AppSettings["TextFontFamily"];
                if (!string.IsNullOrEmpty(fontFamilySource))
                {
                    TextFontFamily = new FontFamily(fontFamilySource);
                }
                // 从配置文件中读取背景颜色
                if (ColorConverter.ConvertFromString(ConfigurationManager.AppSettings["BackgroundColor"]) is Color backgroundColor)
                {
                    BackgroundColor = backgroundColor;
                }
                // 从配置文件中读取背景透明度
                if (double.TryParse(ConfigurationManager.AppSettings["BackgroundOpacity"], out double backgroundOpacity))
                {
                    BackgroundOpacity = backgroundOpacity;
                }
                if (double.TryParse(ConfigurationManager.AppSettings["WindowWidth"], out double width))
                {
                    WindowWidth = width;
                }
                if (double.TryParse(ConfigurationManager.AppSettings["WindowHeight"], out double height))
                {
                    WindowHeight = height;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载用户属性时出错: {ex.Message}");
            }
        }
        protected override void OnStartup(StartupEventArgs e)
        {
           
            base.OnStartup(e);
            LoadUserProperties();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            //SaveUserProperties();
        }
        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
